﻿using System;
using System.Collections.Generic;
using System.Text;
using Manager.App_Code.IDAL;
using Manager.App_Code.DTO;
using System.Data;

namespace Manager.App_Code.DAL
{
    class NhomNguoiDungDAL:StandardConnectDB,INhomNguoiDung
    {
        public List<DTO.NhomNguoiDungDTO> GetlistNhomNguoiDung()
        {
            List<NhomNguoiDungDTO> listNhomNguoiDung = new List<NhomNguoiDungDTO>();
            DataTable dt = DTExecuteSelect("Select * from NhomNguoiDung");
            foreach (DataRow dr in dt.Rows)
            {
                listNhomNguoiDung.Add(ConvertObjectDataRowToNhomNguoiDungDTO(dr));
            }
            return listNhomNguoiDung;
            
        }

        public DTO.NhomNguoiDungDTO GetNhomNguoiDungByID(string MaNhom)
        {
            NhomNguoiDungDTO nhomND = new NhomNguoiDungDTO();
            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaNhom", MaNhom);

            DataTable dt = this.DTExecuteSelect("SP_NhomNguoiDung_GetByID", paramlist);

            nhomND = ConvertObjectDataRowToNhomNguoiDungDTO(dt.Rows[0]);

            return nhomND;
            
        }

        public bool InsertNhomNguoiDung(DTO.NhomNguoiDungDTO nhomnguoidung)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaNhom", nhomnguoidung.MaNhom);
            paramlist.Add("@TenNhom", nhomnguoidung.TenNhom);
            return ExecuteUpdate("SP_NhomNguoiDung_Insert", paramlist);
            
        }

        public bool DeleteNhomNguoiDung(DTO.NhomNguoiDungDTO nhomnguoidung)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaNhom", nhomnguoidung.MaNhom);
            return ExecuteUpdate("SP_NhomNguoiDung_Delete", paramlist);
            
        }

        public bool UpdateNhomNguoiDung(DTO.NhomNguoiDungDTO nhomnguoidung)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaNhom", nhomnguoidung.MaNhom);
            paramlist.Add("@TenNhom", nhomnguoidung.TenNhom);
            return ExecuteUpdate("SP_NhomNguoiDung_Update", paramlist);
            
        }

        public DTO.NhomNguoiDungDTO ConvertObjectDataRowToNhomNguoiDungDTO(System.Data.DataRow dr)
        {
            if (dr != null)
            {
                NhomNguoiDungDTO nhomND = new NhomNguoiDungDTO();
                nhomND.MaNhom = dr["MaNhom"].ToString();
                nhomND.TenNhom = dr["TenNhom"].ToString();

                return nhomND;
            }
            else
                return null;
            
        }
    }
}
